package cn.jiahui;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;

import com.jfinal.kit.JsonKit;

public class LiftReadThread extends Thread {
	Socket socket;
	String hostAddress = "";
	byte heatBeat[] = new byte[1];
	byte btnSateaData[] = new byte[9];
	byte listSateData[] = new byte[11];
	byte setSateData[] = new byte[10];

	BufferedInputStream bis = null;

	public LiftReadThread(Socket s, LiftService liftService) {
		super();

		this.socket = s;
		hostAddress = socket.getInetAddress().getHostAddress();
		Arrays.fill(heatBeat, (byte) 0);
		Arrays.fill(btnSateaData, (byte) 0);
		Arrays.fill(listSateData, (byte) 0);
		Arrays.fill(setSateData, (byte) 0);

		InputStream inputStream = null;

		try {
			inputStream = socket.getInputStream();
			bis = new BufferedInputStream(inputStream);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		try {

			if (bis != null) {

				byte[] head = new byte[2];
				while (true) {
					Arrays.fill(head, (byte) 0);
					int sizeHeadReaded = bis.read(head);
					if (sizeHeadReaded < 2) {
						System.out.println("head size < 2");
						break;
					} else {
						System.out.println("head:" + head[0] + " " + head[1]);
					}
					// 心跳包头,DTU send 心跳包是0x4A,0x48, 0x54, (ASCII : JHT), 间隔10s，
					if (head[0] == 0x4A && head[1] == 0x48) {
						readHeatBeat(bis);
						continue;
					}

					// 按钮的状态.DTU ask Data[10]
					if (head[0] == (byte) 0xA1 && head[1] == (byte) 0xA2) {
						boolean flag = readBtnState(bis);
						if (flag) {
							// TOOD 用另一个socket去读取真实电梯的数据.
						}
						// 把真实电梯的数据发到web
						// sendCommondReadBtnState(bos);
						continue;
					}

					// 电梯的状态.DTU ask Data[13]
					if (head[0] == (byte) 0xB1 && head[1] == (byte) 0xB2) {
						readLiftState(bis);
						continue;
					}

					// 设置命令 DTU ask Data[10]
					if (head[0] == (byte) 0x03 && head[1] == (byte) 0x6E) {
						// 和读取按钮的协议是一样的.
						readSetState(bis);

						continue;
					}

					// 退出命令
					// if(bs[0] == 'q' && bs[1] == 'u'){
					// StringBuilder sb = new StringBuilder();
					// String strWord = bis.readLine();
					// sb.append(bs[0]).append(bs[1])
					// .append(strWord);
					// if ("quit".equalsIgnoreCase(sb.toString())) {
					// System.out.println("quit");
					//
					// break;
					// }
					//
					// }

				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	

	public boolean readHeatBeat(BufferedInputStream bis) throws IOException {
		int sizeToRead = 1;
		Arrays.fill(heatBeat, (byte) 0);
		int beatSize = bis.read(heatBeat);
		if (beatSize == sizeToRead) {
			System.out.println("beat bytes readed");
			HibUtils.printByte(heatBeat);
			// String result = HibUtils.doGet(prefixUrl+"hartBeat");
			// System.out.println( result);
			return true;
		} else {
			System.out.println("beat bytes readed failed");
		}
		return false;
	}

	public boolean readBtnState(BufferedInputStream bis) throws IOException {
		int sizeToRead = 9;
		Arrays.fill(btnSateaData, (byte) 0);
		int beatSize = bis.read(btnSateaData);
		if (beatSize == sizeToRead) {
			System.out.println("button state bytes readed");
			HibUtils.printByte(btnSateaData);
			// String result = HibUtils.doGet(prefixUrl+"socketAck/?result=ok");
			// System.out.println( result);
			return true;

		} else {
			System.out.println("button state bytes readed failed");
			// String result =
			// HibUtils.doGet(prefixUrl+"jiajing/socketAck/?result=failed");
			// System.out.println( result);
		}
		return false;
	}

	public boolean readLiftState(BufferedInputStream bis) throws IOException {
		int sizeToRead = 11;
		Arrays.fill(listSateData, (byte) 0);
		int beatSize = bis.read(listSateData);
		if (beatSize == sizeToRead) {
			System.out.println("lift state bytes readed");
			HibUtils.printByte(listSateData);
			return true;
		} else {
			System.out.println("lift state bytes readed failed");
		}
		return false;
	}

	public boolean readSetState(BufferedInputStream bis) throws IOException {
		int sizeToRead = 10;
		Arrays.fill(setSateData, (byte) 0);
		int beatSize = bis.read(setSateData);
		if (beatSize == sizeToRead) {
			System.out.println("set state bytes readed");
			HibUtils.printByte(setSateData);
			return true;
		} else {
			System.out.println("set state bytes readed failed");
		}
		return false;
	}

}
